package com.llkj.qingnianguoxue.adapter;

import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.bean.JingXuanBean;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.TextViewUtil;
import com.xiaweizi.cornerslibrary.CornersProperty;

import java.util.List;

/**
 * 首页——课程
 */
public class HomeJXCourseAdapter extends BaseQuickAdapter<JingXuanBean,BaseViewHolder> {
    private int type = 1;//1课程，2活动

    public HomeJXCourseAdapter(int layoutResId, List<JingXuanBean> data) {
        super(layoutResId, data);
    }

    @Override
    public int getItemCount() {
        int count = 0;
        if(mData!=null){
                count = mData.size()>2?2:mData.size();
        }
        return count;
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, JingXuanBean item) {
        ImageView ivPic = baseViewHolder.getView(R.id.iv_pic);
        baseViewHolder.setText(R.id.tv_title, item.getName());

        TextView tvPrice = baseViewHolder.getView(R.id.tv_price);
        if(!StringUtils.isEmpty(item.getPrice())){
            String money = "￥" + item.getPrice()+"/时";
            SpannableString strPrice = TextViewUtil.getForegroundColorSpan(mContext,money,money.length()-2,money.length(), ContextCompat.getColor(mContext, R.color.color999999));
            tvPrice.setText(strPrice);
        }else{
            tvPrice.setText("");
        }

        String picUrl = item.getImg();
        String time = item.getStart_time();
        ImageLoadingUtil.loadImgGlideRound(ivPic,picUrl ,
                5, CornersProperty.CornerType.TOP);
        if(!StringUtils.isEmpty(time)){
            try {
                time = time.substring(0,10);
            }catch (Exception e){}
        }
        baseViewHolder.setText(R.id.tv_date, time);

        TextView tvState = baseViewHolder.getView(R.id.tv_state);

        boolean isReverse = !"3".equals(item.getIs_reserve())?true:false;
        tvState.setSelected(isReverse);
        if("2".equals(item.getIs_reserve())){
            tvState.setText("满额");
        }else if("1".equals(item.getIs_reserve())){
            tvState.setText("已预约");
        }else{
            tvState.setText("预约");
        }

        RelativeLayout llRoot = baseViewHolder.getView(R.id.ll_root);
        LinearLayout.LayoutParams rootParams = (LinearLayout.LayoutParams) llRoot.getLayoutParams();
        RelativeLayout.LayoutParams picParams = (RelativeLayout.LayoutParams) ivPic.getLayoutParams();
        picParams.width = (DisplayUtil.getDensityWdith(mContext)-DisplayUtil.dip2px(mContext, 40))/2;
        picParams.height = picParams.width/2;
//
        rootParams.width = picParams.width;
    }

    public void setType(int type1){
        type = type1;
    }
}
